package com.pjbao.Service.util;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.pjbao.Service.ReportService;
import com.pjbao.Service.model.PersonInfoRecord;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import sun.misc.BASE64Encoder;

import java.util.Map;

/**
 * Created by zero on 2016/9/27.
 */

@Service
public class ControllerUtil {

    Logger logger = LoggerFactory.getLogger("api service v1");
    @Autowired
    ReportService reportService;


    public String personalInvoke(PersonInfoRecord info, MultipartFile files) throws Exception{
        String result = "";
        try {
            if (!files.isEmpty()) {
                BASE64Encoder encoder = new BASE64Encoder();
                String data = encoder.encode(files.getBytes());
                info.setPhoto(data);
            }
        } catch (Exception e) {

        }
        String g1Exist="";
        String[] selectedInterface = info.getSelectInterface().split("~");
        for(int i=0; i<selectedInterface.length; i++){
            if("g1".equals(selectedInterface[i])){
                g1Exist = "g1true";
            }
        }

        if(!g1Exist.equals("g1true")){
            logger.info("进入非G1接口");
            Map<String, Object> resultMap = reportService.exportCode(info);
            resultMap.put("cid", info.getCid());
            resultMap.put("cardID", info.getCardID());
            resultMap.put("name", info.getName());
            resultMap.put("month", info.getMonth());
            resultMap.put("phone", info.getPhone());
            ObjectMapper mapper = new ObjectMapper();
            result = mapper.writeValueAsString(resultMap);
            logger.info("===============\n"+result);
        }else{
            logger.info("用户名:"+info.getName()+info.getCid()+"===token:"+info.getUserid());
            Map<String, Object> resultMap = reportService.exportWordByType(info);
            resultMap.put("cid", info.getCid());
            resultMap.put("cardID", info.getCardID());
            resultMap.put("name", info.getName());
            resultMap.put("month", info.getMonth());
            resultMap.put("phone", info.getPhone());
            ObjectMapper mapper = new ObjectMapper();
            result = mapper.writeValueAsString(resultMap);
            logger.info("===============\n"+result);
        }

        return result;
    }

    public String companyInvoke(PersonInfoRecord info) throws Exception{
        Map<String, Object> resultMap = reportService.companyExport(info);
        resultMap.put("comName", info.getComName());
        ObjectMapper mapper = new ObjectMapper();
        String result = mapper.writeValueAsString(resultMap);
        return result;
    }


}
